home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Python / errors.c < prev    next >
C/C++ Source or Header  |  1998-06-24  |  6KB  |  206 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Error handling -- see also run.c */
  33.  
  34. /* New error handling interface.
  35.  
  36.    The following problem exists (existed): methods of built-in modules
  37.    are called with 'self' and 'args' arguments, but without a context
  38.    argument, so they have no way to raise a specific exception.
  39.    The same is true for the object implementations: no context argument.
  40.    The old convention was to set 'errno' and to return NULL.
  41.    The caller (usually call_function() in eval.c) detects the NULL
  42.    return value and then calls puterrno(ctx) to turn the errno value
  43.    into a true exception.  Problems with this approach are:
  44.    - it used standard errno values to indicate Python-specific errors,
  45.      but this means that when such an error code is reported by a system
  46.      call (e.g., in module posix), the user gets a confusing message
  47.    - errno is a global variable, which makes extensions to a multi-
  48.      threading environment difficult; e.g., in IRIX, multi-threaded
  49.      programs must use the function oserror() instead of looking in errno
  50.    - there is no portable way to add new error numbers for specic
  51.      situations -- the value space for errno is reserved to the OS, yet
  52.      the way to turn module-specific errors into a module-specific
  53.      exception requires module-specific values for errno
  54.    - there is no way to add a more situation-specific message to an
  55.      error.
  56.   
  57.   The new interface solves all these problems.  To return an error, a
  58.   built-in function calls err_set(exception), err_setval(exception,
  59.   value) or err_setstr(exception, string), and returns NULL.  These
  60.   functions save the value for later use by puterrno().  To adapt this
  61.   scheme to a multi-threaded environment, only the implementation of
  62.   err_setval() has to be changed.
  63. */
  64.  
  65. #include "allobjects.h"
  66. #include "traceback.h"
  67.  
  68. #include <errno.h>
  69.  
  70. #ifdef SYMANTEC__CFM68K__
  71. #pragma lib_export on
  72. #endif
  73.  
  74. #ifdef macintosh
  75. /* Replace strerror with a Mac specific routine.
  76.    XXX PROBLEM: some positive errors have a meaning for MacOS,
  77.    but some library routines set Unix error numbers...
  78. */
  79. extern char *PyMac_StrError PROTO((int));
  80. #undef strerror
  81. #define strerror PyMac_StrError
  82. #endif /* macintosh */
  83.  
  84. #ifndef __STDC__
  85. extern char *strerror PROTO((int));
  86. #endif
  87.  
  88. /* Last exception stored by err_setval() */
  89.  
  90. static object *last_exception;
  91. static object *last_exc_val;
  92.  
  93. void
  94. err_restore(exception, value, traceback)
  95.     object *exception;
  96.     object *value;
  97.     object *traceback;
  98. {
  99.     err_clear();
  100.  
  101.     last_exception = exception;
  102.     last_exc_val = value;
  103.     (void) tb_store(traceback);
  104.     XDECREF(traceback);
  105. }
  106.  
  107. void
  108. err_setval(exception, value)
  109.     object *exception;
  110.     object *value;
  111. {
  112.     XINCREF(exception);
  113.     XINCREF(value);
  114.     err_restore(exception, value, (object *)NULL);
  115. }
  116.  
  117. void
  118. err_set(exception)
  119.     object *exception;
  120. {
  121.     err_setval(exception, (object *)NULL);
  122. }
  123.  
  124. void
  125. err_setstr(exception, string)
  126.     object *exception;
  127.     char *string;
  128. {
  129.     object *value = newstringobject(string);
  130.     err_setval(exception, value);
  131.     XDECREF(value);
  132. }
  133.  
  134.  
  135. object *
  136. err_occurred()
  137. {
  138.     return last_exception;
  139. }
  140.  
  141. void
  142. err_fetch(p_exc, p_val, p_tb)
  143.     object **p_exc;
  144.     object **p_val;
  145.     object **p_tb;
  146. {
  147.     *p_exc = last_exception;
  148.     last_exception = NULL;
  149.     *p_val = last_exc_val;
  150.     last_exc_val = NULL;
  151.     *p_tb = tb_fetch();
  152. }
  153.  
  154. void
  155. err_clear()
  156. {
  157.     object *tb;
  158.     XDECREF(last_exception);
  159.     last_exception = NULL;
  160.     XDECREF(last_exc_val);
  161.     last_exc_val = NULL;
  162.     /* Also clear interpreter stack trace */
  163.     tb = tb_fetch();
  164.     XDECREF(tb);
  165. }
  166.  
  167. /* Convenience functions to set a type error exception and return 0 */
  168.  
  169. int
  170. err_badarg()
  171. {
  172.     err_setstr(TypeError, "illegal argument type for built-in operation");
  173.     return 0;
  174. }
  175.  
  176. object *
  177. err_nomem()
  178. {
  179.     err_set(MemoryError);
  180.     return NULL;
  181. }
  182.  
  183. object *
  184. err_errno(exc)
  185.     object *exc;
  186. {
  187.     object *v;
  188.     int i = errno;
  189. #ifdef EINTR
  190.     if (i == EINTR && sigcheck())
  191.         return NULL;
  192. #endif
  193.     v = mkvalue("(is)", i, strerror(i));
  194.     if (v != NULL) {
  195.         err_setval(exc, v);
  196.         DECREF(v);
  197.     }
  198.     return NULL;
  199. }
  200.  
  201. void
  202. err_badcall()
  203. {
  204.     err_setstr(SystemError, "bad argument to internal function");
  205. }
  206.